package com.example.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.operation.domin.DispatchCity;
import com.example.operation.domin.DispatchExpress;
import com.example.operation.dto.DispatchCityDto;
import com.example.operation.dto.DispatchExpressDto;
import com.example.operation.mapper.DispatchExpressMapper;
import com.example.operation.service.DispatchExpressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: DispatchExpressServiceImpl
 * @Description:
 * @Author
 * @Date 2021/11/24
 * @Version 1.0
 */


@Service
public class DispatchExpressServiceImpl extends ServiceImpl<DispatchExpressMapper, DispatchExpress> implements DispatchExpressService {

    @Autowired
    private DispatchExpressMapper dispatchExpressMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(DispatchExpressDto dispatchExpressDto) {
        DispatchExpress dispatchExpress = new DispatchExpress();
        LambdaUpdateWrapper<DispatchExpress> lambda = new UpdateWrapper<DispatchExpress>()
                .lambda().eq(DispatchExpress::getDispatchId,dispatchExpressDto.getDispatchId());
        Integer count = dispatchExpressMapper.selectCount(lambda);
        if(count ==0) {
            throw new RuntimeException("当前配送方式有误,请修改之后再执行");
        }
        BeanUtils.copyProperties(dispatchExpressDto,dispatchExpress);
        dispatchExpress.setAddress(dispatchExpressDto.getAddress().toString());
        return dispatchExpressMapper.update(dispatchExpress,lambda);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delectDyId(Integer id) {
        return dispatchExpressMapper.deleteById(id);
    }
}
